package cn.edu.java.io.writer;

import java.io.StringWriter;
import java.io.PrintWriter;

public class StringWriterDemo {
    public static void main(String[] args) {
        try (StringWriter stringWriter = new StringWriter();
             PrintWriter printWriter = new PrintWriter(stringWriter);) {


            printWriter.println("Hello, World!");
            printWriter.println("This is a test message.");
            printWriter.printf("Number: %d", 42);

            String result = stringWriter.toString();
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

